package com.momo.hotel.controller;

import com.momo.hotel.dao.pojo.TbCommonProblems;
import com.momo.hotel.dao.pojo.TbQuestion;
import com.momo.hotel.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author qiang
 * @create 2019/7/4
 */
@Controller
public class QuestionController {

    @Resource
    QuestionService questionService;

    /**
     * 客户提问
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("addQuestion")
    public void addQuestion(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        String username = request.getParameter("username");
        String email = request.getParameter("email");
        String question = request.getParameter("question");
        TbQuestion tbQuestion = new TbQuestion();
        tbQuestion.setUsername(username);
        tbQuestion.setEmail(email);
        tbQuestion.setQuestion(question);
        Integer integer = questionService.addQuestion(tbQuestion);
        if (integer != 0){
            response.getWriter().write("<script>alert('发送成功');location.href=\"faq.html\";</script>");
        }else {
            response.getWriter().write("<script>alert('发送失败');</script>");
        }
    }

    /**
     * 常见问题
     * @return
     */
    @ResponseBody
    @RequestMapping("selectCommonProblems")
    public Object selectCommonProblems(){
        List<TbCommonProblems> tbCommonProblems = questionService.selectCommonProblems();
        return tbCommonProblems;
    }
}
